<?php
namespace app\common\service;

use app\common\model\UserToken;
use app\common\model\WxloginMember;
use EasyWeChat\Factory;

class WechatLoginService
{
    
    /**
     * 用户登录
     *
     * @param string $code 小程序code码
     * @return json
     */
    public static function login($code = '')
    {
        $config = config('wechat.miniProgram');
        
        $config = [
            'app_id' => $config['app_id'],
            'secret' => $config['secret'],
            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/wechat.log',
            ],
        ];
        $code = (string) $code;
        if (empty($code)) {
            return JsonService::fail('code不能为空');
        }
        $app = Factory::miniProgram($config);
        
        $info = $app->auth->session((string)$code);
       
        $openid = $info['openid'] ?? '';
        
        if (empty($openid)) {
            return JsonService::fail('openid不能为空');
        }
        $user_info = WxloginMember::getUserByUnionid($info['unionid']);
       
        if(!empty($user_info['user_id'])){

            $user = model('User')->getUinfo(['userid' => $user_info['user_id']]);

            $token = UserToken::where('userid',$user_info['user_id'])->where('platform','miniapp')->find();

            if(empty($token)){
                $info['token'] = model('UserToken')->create_token($user['userid'],$user['username'],'miniapp');
            }else{
                $userInfo = model('UserToken')->check_token($token['token'],'miniapp');
                
                if($userInfo){
                    $info['token'] = $token['token'];
                }
            }
            
            return JsonService::successful($info);
        }else{
            $info['token'] = '';
            return JsonService::successful($info);
        }
        
    }


    /**
     * 微信解密
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2021-12-06
     * @param string $session
     * @param string $iv
     * @param string $encryptedData
     * @return mixed
     */
    public static function decryptData($session, $iv, $encryptedData){
        $config = config('wechat.miniProgram');
        $config = [
            'app_id' => $config['app_id'],
            'secret' => $config['secret'],
            'response_type' => 'array',
        ];
       
        $app = Factory::miniProgram($config);
        
        $decryptedData = $app->encryptor->decryptData($session, $iv, $encryptedData);
       
        if (!$decryptedData) {
            return [];
        }
        if (!empty($decryptedData)) {
            return $decryptedData;
        }
    }

    /**
     * 获取sessionKey
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2021-12-07
     * @param string $code
     * @return mixed
     */
    public static function getSessionKey($code = ''){
        $config = config('wechat.miniProgram');
        
        $config = [
            'app_id' => $config['app_id'],
            'secret' => $config['secret'],
            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/wechat.log',
            ],
        ];
        $code = (string) $code;
        if (empty($code)) {
            return JsonService::fail('code不能为空');
        }
        $app = Factory::miniProgram($config);
        
        $info = $app->auth->session((string)$code);
        if(!empty($info)){
            return $info;
        }
    }


}